﻿using Store.Manager.ERP.Application.Dtos;
using Store.Manager.ERP.Common.Models;
using Store.Manager.ERP.Domain.Entities;
using Store.Manager.ERP.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Store.Manager.ERP.Application.Services
{
    public class UserService : IUserService
    {
        private readonly IUserRepository _userRepository;

        public UserService(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public async Task<CommonResponse<UserDto>> LoginAsync(string username, string password)
        {
            var user = await _userRepository.GetUserByUsernameAsync(username);
            if (user != null && user.Password == password)
            {
                var resDto = new UserDto()
                {
                    Uid = user.Id,
                    Username = user.Username
                };
                return CommonResponse<UserDto>.OK(resDto);
            }
            return CommonResponse<UserDto>.Error("用户名密码不正确");
        }

        public async Task RegisterAsync(UserDto userDto)
        {
            var user = new User
            {
                Username = userDto.Username,
                Password = userDto.Password,
                Email = userDto.Email
            };
            await _userRepository.AddUserAsync(user);
        }
    }
}
